﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using FirstFloor.Documents.IO;
using FirstFloor.Documents.Presentation;

namespace SamplePresentation
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();

            // create view model (defined in FirstFloor.Documents.Presentation)
            var viewModel = new DocumentViewModel(this.DataSource);
            
            // load presentation document
            this.DataSource.PackageReader = new DefaultPackageReader(new Uri("Presentations/Document Toolkit.xps", UriKind.Relative));

            // assign to data context
            this.LayoutRoot.DataContext = viewModel;
        }

        private void LayoutRoot_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            if (ContentControl.Content == this.Thumbnails) {
                return;
            }

            if (e.Delta < 0) {
                this.Navigator.PageIndex++;
            }
            else {
                this.Navigator.PageIndex--;
            }
        }

        private void ShowViewer(bool show)
        {
            if (show) {
                this.ContentControl.Content = this.PageViewerBorder;
            }
            else {
                var parent = this.Thumbnails.Parent as Panel;
                if (parent != null) {
                    this.ThumbnailsTranslate.X = 0;
                    parent.Children.Remove(this.Thumbnails);
                }

                this.ContentControl.Content = this.Thumbnails;
            }
        }

        private void ToggleContent_Click(object sender, RoutedEventArgs e)
        {
            this.ToggleContent.IsEnabled = false;
            ShowViewer(this.ContentControl.Content != this.PageViewerBorder);
        }

        private void Content_TransitionCompleted(object sender, RoutedEventArgs e)
        {
            this.ToggleContent.IsEnabled = true;
        }

        private void Border_KeyDown(object sender, KeyEventArgs e)
        {
            if (ContentControl.Content == this.Thumbnails) {
                return;
            }

            if (e.Key == Key.PageDown || e.Key == Key.Down || e.Key == Key.Right) {
                this.Navigator.PageIndex++;
            }
            else if (e.Key == Key.PageUp || e.Key == Key.Up || e.Key == Key.Left) {
                this.Navigator.PageIndex--;
            }
        }
    }
}
